<template>
  <div class="autoPublish">
    <a-modal :visible="dialogVisible" title="设置自动发布" @ok="handOk" @cancel="handCancel" :width="'600px'">
      <div class="switchBox">
        <a-switch v-model="publishStatus" />
        <span class="switch">是否设置自动发布</span>
      </div>
      <div class="inputNumberBox">
        <a-input-number class="inputNumber" v-model="days" :min="0" :max="100000" :allowClear='true'/>
        <span class="numberText">
          （根据任务的目标开始时间设置提前发布的天数。）
        </span>
      </div>
    </a-modal>
  </div>
</template>
<script>
let userinfo = JSON.parse(sessionStorage.getItem('userInfo'))
let tenantId = ''
if (userinfo) {
  tenantId = userinfo.tenantId
}
import { getPushTaskSet, setPushTaskSet }  from "@api/projectPlan";
export default {
  name: 'autoPublish',
  data() {
    return {
      publishStatus: false,
      days: 1,
    }
  },
  props: ['dialogVisible'],
    computed: {
        planId() {
          const tempId = sessionStorage.getItem('PIPlanId')
          return tempId.substring(tempId.lastIndexOf(':') + 1,tempId.length)
        }
    },
  watch: {
    dialogVisible(val, oldVal) {
      if (val) {
        this.init()
      }
    }
  },
  methods: {
    init() {
      this.getPushTaskInfo()
    },
    getPushTaskInfo() {
      let data = {
        oid: this.planId
      }
      getPushTaskSet(data).then(res => {
        if (res && res.status === 200) {
          if (res.data && res.data.length > 0) {
            this.days = res.data[0].days
            this.publishStatus = res.data[0].onOff
          }
        }
      })
    },
    setPushTask() {
      let data = {
        "days": this.days,
        "oid": this.planId,
        "onOff": this.publishStatus,
        "tenantId": tenantId,
      }
      setPushTaskSet(data).then(res => {
        if (res && res.status === 200) {
          this.$message.success('自动发布成功')
        } else {
          this.$message.error('自动发布失败')
        }
      })
    },
    handOk() {
      this.setPushTask()
      this.$emit('ok')
    },
    handCancel() {
      this.$emit('cancel')
    }
  }
}
</script>
<style scoped>
.switchBox {
  display: flex;
  align-items: center;
}
.switchBox .switch {
  margin-left: 10px;
}
.inputNumberBox {
  margin-top: 10px;
}
.inputNumberBox .inputNumber {
  margin: 0px 10px 0px 55px;
  width: 140px;
}
.inputNumberBox .numberText {
  font-size: 12px;
  color: #386BD7;
}
</style>